package cn.lblbc.calc

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class SettingsActivity : AppCompatActivity() {

    private lateinit var themeSpinner: Spinner
    private lateinit var aiProgrammingLayout: View

    override fun onCreate(savedInstanceState: Bundle?) {
        // 应用保存的主题
        applySavedTheme()
        
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_settings)
        
        setSupportActionBar(findViewById(R.id.toolbar))
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.title = "设置"
        
        initializeViews()
        setupThemeSpinner()
        setupListeners()
    }

    private fun initializeViews() {
        themeSpinner = findViewById(R.id.theme_spinner)
        aiProgrammingLayout = findViewById(R.id.aiProgrammingLayout)
    }

    private fun setupThemeSpinner() {
        val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, CalculatorActivity.THEME_NAMES)
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        themeSpinner.adapter = adapter
        
        // 设置当前选中的主题
        val prefs = getSharedPreferences(CalculatorActivity.PREFS_NAME, MODE_PRIVATE)
        val currentTheme = prefs.getString(CalculatorActivity.THEME_KEY, CalculatorActivity.THEME_CYBERPUNK) ?: CalculatorActivity.THEME_CYBERPUNK
        val currentThemeIndex = CalculatorActivity.THEME_KEYS.indexOf(currentTheme)
        if (currentThemeIndex >= 0) {
            themeSpinner.setSelection(currentThemeIndex)
        }
        
        // 设置选择监听器
        themeSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) {
                val selectedTheme = CalculatorActivity.THEME_KEYS[position]
                if (selectedTheme != currentTheme) {
                    // 保存主题选择
                    prefs.edit().putString(CalculatorActivity.THEME_KEY, selectedTheme).apply()
                    // 重新创建Activity以应用新主题
                    recreate()
                }
            }
            
            override fun onNothingSelected(parent: AdapterView<*>) {
                // 什么都不做
            }
        }
    }

    private fun setupListeners() {
        aiProgrammingLayout.setOnClickListener {
            val url = "https://cxyxy.blog.csdn.net/article/details/151590489"
            val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
            startActivity(intent)
        }
    }

    private fun applySavedTheme() {
        val prefs = getSharedPreferences(CalculatorActivity.PREFS_NAME, MODE_PRIVATE)
        val currentTheme = prefs.getString(CalculatorActivity.THEME_KEY, CalculatorActivity.THEME_CYBERPUNK) ?: CalculatorActivity.THEME_CYBERPUNK
        
        when (currentTheme) {
            CalculatorActivity.THEME_CYBERPUNK -> setTheme(R.style.Theme_Calculator_Cyberpunk)
            CalculatorActivity.THEME_CLASSIC -> setTheme(R.style.Theme_Calculator_Classic)
            CalculatorActivity.THEME_OCEAN -> setTheme(R.style.Theme_Calculator_Ocean)
            CalculatorActivity.THEME_SUNSET -> setTheme(R.style.Theme_Calculator_Sunset)
            CalculatorActivity.THEME_FOREST -> setTheme(R.style.Theme_Calculator_Forest)
            CalculatorActivity.THEME_MINIMALIST -> setTheme(R.style.Theme_Calculator_Minimalist)
            CalculatorActivity.THEME_DEEPSPACE -> setTheme(R.style.Theme_Calculator_DeepSpace)
            CalculatorActivity.THEME_CRT -> setTheme(R.style.Theme_Calculator_CRT)
            CalculatorActivity.THEME_CANDY -> setTheme(R.style.Theme_Calculator_Candy)
            CalculatorActivity.THEME_FIRE -> setTheme(R.style.Theme_Calculator_Fire)
        }
    }

    override fun onSupportNavigateUp(): Boolean {
        finish()
        return true
    }
}